package com.drinkian.authentication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.drinkian.authentication.entity.po.Role;
import com.drinkian.authentication.entity.po.UserRole;
import com.drinkian.authentication.entity.vo.VRoleListWithUserId;
import com.drinkian.authentication.mapper.RoleMapper;
import com.drinkian.authentication.mapper.UserRoleMapper;
import com.drinkian.authentication.service.IRoleService;
import com.drinkian.entity.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public RequestResult getAllRole() {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("status", 1);
        List<Role> roleList = roleMapper.selectList(roleQueryWrapper);
        return new RequestResult(HttpStatus.OK.value(), roleList);
    }

    @Override
    public RequestResult changeUserRole(VRoleListWithUserId vRoleListWithUserId) {
        Long userId = vRoleListWithUserId.getUserId();

        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id", userId);
        // 原本的角色列表
        List<UserRole> userRoleList = userRoleMapper.selectList(userRoleQueryWrapper);

        Set<Long> origin = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
        Set<Long> current = vRoleListWithUserId.getRoleList().stream().collect(Collectors.toSet());

        // 交集，被删除的就重置
        HashSet<Long> union = new HashSet<>();
        union.addAll(origin);
        union.containsAll(current);
        for (Long roleId : union) {
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("role_id", roleId);
            UserRole userRole = userRoleMapper.selectOne(queryWrapper);
            if (userRole.getDeleted() == 1) {
                userRole.setDeleted(0);
                userRoleMapper.updateById(userRole);
            }
        }

        // 需要删除的差集
        HashSet<Long> deleteDiff = new HashSet<>();
        deleteDiff.addAll(origin);
        deleteDiff.removeAll(current);
        for (Long roleId : deleteDiff) {
            UpdateWrapper<UserRole> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", userId);
            updateWrapper.eq("role_id", roleId);
            updateWrapper.set("deleted", 1);
            userRoleMapper.update(null, updateWrapper);
        }

        // 需要新加的差集
        HashSet<Long> insertDiff = new HashSet<>();
        insertDiff.addAll(current);
        insertDiff.removeAll(origin);
        for (Long roleId : insertDiff) {
            UserRole userRole = new UserRole(null, userId, roleId, null, null, null, null);
            userRoleMapper.insert(userRole);
        }

        return new RequestResult(HttpStatus.OK.value(), "修改成功!");
    }
}
